package com.ssy.lingxi.workflow.entity;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.ssy.lingxi.workflow.handler.converter.JpaJsonToKeyValueBOConverter;
import com.ssy.lingxi.workflow.model.bo.KeyValueBO;

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
 * （内部）流程定义
 * @author 万宁
 * @version 2.0.0
 * @date 2021-04-21
 */
@Entity
@Table(schema = "public", name = "wf_internal_task_definition", indexes = {@Index(name = "wf_internal_task_definition_process_key_idx", columnList = "processKey"), @Index(name = "wf_internal_task_definition_task_type_idx", columnList = "taskType"), @Index(name = "wf_internal_task_definition_task_name_idx", columnList = "taskName")})
public class InternalTaskDefinition implements Serializable {
    private static final long serialVersionUID = -8194334467622914655L;

    /**
     * 主键名称id
     */
    @Id
    @GeneratedValue(strategy= GenerationType.SEQUENCE, generator="internal_task_definition_seq")
    @SequenceGenerator(sequenceName="wf_internal_task_definition_seq", name="internal_task_definition_seq",allocationSize = 1)
    private Long id;

    /**
     * 流程Key
     */
    @Column(columnDefinition = "varchar(100)")
    private String processKey;

    /**
     * 流程名称
     */
    @Column(columnDefinition = "varchar(50)")
    private String processName;

    /**
     * 流程中任务的步骤
     */
    @Column
    private Integer taskStep;

    /**
     * 步骤名称
     */
    @Column(columnDefinition = "varchar(50)")
    private String taskName;

    /**
     * 任务类型，定义在TaskTypeEnum中
     */
    @Column
    private Integer taskType;

    /**
     * 跳转条件，如果没有则为-1
     */
    @Column
    private Integer agree;

    /**
     * 关联的前端页面Url
     */
    @Column(columnDefinition = "varchar(400)")
    private String url;

    /**
     * Task执行后返回的状态
     */
    @Column
    private Integer status;

    /**
     * Task执行后返回的状态的描述
     */
    @Column(columnDefinition = "varchar(40)")
    private String statusDesc;

    /**
     * 自定义的Extensions属性
     */
    @Convert(converter = JpaJsonToKeyValueBOConverter.class)
    @Column(columnDefinition = "jsonb")
    private List<KeyValueBO> other;

    /**
     * 多对一双向关联外部任务
     */
    @JsonBackReference
    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.REMOVE}, fetch = FetchType.LAZY)
    @JoinColumn(name = "external_task_id")
    private ExternalTaskDefinition extTask;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getProcessKey() {
        return processKey;
    }

    public void setProcessKey(String processKey) {
        this.processKey = processKey;
    }

    public String getProcessName() {
        return processName;
    }

    public void setProcessName(String processName) {
        this.processName = processName;
    }

    public Integer getTaskStep() {
        return taskStep;
    }

    public void setTaskStep(Integer taskStep) {
        this.taskStep = taskStep;
    }

    public String getTaskName() {
        return taskName;
    }

    public void setTaskName(String taskName) {
        this.taskName = taskName;
    }

    public Integer getTaskType() {
        return taskType;
    }

    public void setTaskType(Integer taskType) {
        this.taskType = taskType;
    }

    public Integer getAgree() {
        return agree;
    }

    public void setAgree(Integer agree) {
        this.agree = agree;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getStatusDesc() {
        return statusDesc;
    }

    public void setStatusDesc(String statusDesc) {
        this.statusDesc = statusDesc;
    }

    public List<KeyValueBO> getOther() {
        return other;
    }

    public void setOther(List<KeyValueBO> other) {
        this.other = other;
    }

    public ExternalTaskDefinition getExtTask() {
        return extTask;
    }

    public void setExtTask(ExternalTaskDefinition extTask) {
        this.extTask = extTask;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        InternalTaskDefinition that = (InternalTaskDefinition) o;
        return Objects.equals(id, that.id) &&
                Objects.equals(processKey, that.processKey) &&
                Objects.equals(processName, that.processName) &&
                Objects.equals(taskStep, that.taskStep) &&
                Objects.equals(taskName, that.taskName) &&
                Objects.equals(taskType, that.taskType) &&
                Objects.equals(agree, that.agree) &&
                Objects.equals(url, that.url) &&
                Objects.equals(status, that.status) &&
                Objects.equals(statusDesc, that.statusDesc);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, processKey, processName, taskStep, taskName, taskType, agree, url, status, statusDesc);
    }
}
